#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
from common import L
from common.tools import load_env, setEnv, getPO, generatePythonFile


def yml2Python(yml, pages):
    generatePythonFile(pages, getPO(yml))


def run(path_or_file, report, dot_env, pt=None):
    try:
        if dot_env:
            env_var, msg = load_env(dot_env)
            if isinstance(env_var, dict):
                L.info(msg)
                if env_var:
                    setEnv(**env_var)
            elif env_var == 1:
                L.warning(msg)
    except Exception as e:
        L.error(e)
        exit(1)

    xml_name = '{}.xml'.format(report)
    html_name = '{}.html'.format(report)
    args = ['--junitxml={}'.format(xml_name), '--html={}'.format(html_name)]

    if pt:
        newA = ['']
        for i in pt.split():
            if i.startswith('-'):
                newA.append(i)
            else:
                newA[-1] = '{} {}'.format(newA[-1], i)
        args += newA
    args += list(path_or_file)
    args = [x for x in args if x]
    try:
        pytest.main(args)
        result = 0
    except Exception as e:
        result = 1
        L.error(e)

    return result


if __name__ == '__main__':
    pass
